{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "267812d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 列表推导式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ba99e7db",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 从列表数据创建一个新的列表方式如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bf237b4d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算0-9所有偶数的平方\n",
    "squares = [] # 创建一个空列表\n",
    "for x in range(10): # 0-9的数字区间\n",
    "    if x%2 == 0: # 判断是否为偶数\n",
    "        squares.append(x**2) # 将x的平方append到空列表里"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "aa7ee1de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 4, 16, 36, 64]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ffc8860",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 使用列表推导式的方法如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b4309e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#### 空列表名称 = [想要的结果 for循环 if条件进行过滤]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f313b720",
   "metadata": {},
   "outputs": [],
   "source": [
    "squares = [x**2 for x in range(10) if x%2==0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f2ed480a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 4, 16, 36, 64]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d0aad9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 可以有两个for循环遍历两个变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89880456",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 结果元素可以使用各种函数加工"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d86b39a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('0', 0), ('1', 1), ('2', 4), ('3', 9), ('4', 16), ('5', 25)]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[(str(x),x**2) for x in range(6)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56b8f33d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 集合set推导式\n",
    "- 将中括号换成大括号，就变成集合set推导式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "aab2d85a",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = {x for x in 'aabcssdadef' if x not in 'abc'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7f59c776",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'d', 'e', 'f', 's'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f9e12d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典dict推导式\n",
    "- 大括号，返回元素是key：value形式，就变成了字典推导式。结果有key有value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d98d0361",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'2': 4, '4': 16, '6': 36}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回的是一个新的字典\n",
    "{str(x):x**2 for x in (2,4,6)}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
